package org.sprong.generagor.domain.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

import org.sprong.generagor.domain.entity.AddressGenerator;
import org.sprong.generagor.domain.entity.AddressVO;
import org.sprong.generagor.domain.entity.UserVO;

/**
 * @author guoqiang.lv 2021/10/18
 */
public class UserGeneratorService {

    private AddressGenerator addressGenerator;

    public UserGeneratorService() throws InterruptedException, ExecutionException, IOException {
        addressGenerator = new AddressGenerator();
        addressGenerator.parse();
    }

    public UserVO generator() {
        return UserVO.generateRandomUser(addressGenerator);
    }

    public void generator10w(String path) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(path));
        for (AddressVO address : addressGenerator.getAddresses()) {
            writer.write(UserVO.generateRandomUser(address).format());
            writer.newLine();
        }
        writer.flush();
        writer.close();
    }

    public void generator100w(String path) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(path));
        for (int i = 0; i < 10; i++) {
            for (AddressVO address : addressGenerator.getAddresses()) {
                writer.write(UserVO.generateRandomUser(address).format());
                writer.newLine();
            }
        }
        writer.flush();
        writer.close();
    }

    public void generator1000w(String path) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(path));
        for (int i = 0; i < 100; i++) {
            for (AddressVO address : addressGenerator.getAddresses()) {
                writer.write(UserVO.generateRandomUser(address).format());
                writer.newLine();
            }
        }
        writer.flush();
        writer.close();
    }
}
